function [z]=export(varargin)
%
% export (to an output file) a list of from the control worksheet, depending on the argument list
%
% First argument is the output filename.
% remainder of arguments are the variables to be exported
% datafilename and record are always exported.
%
fout = 1;
[foname, fopath] = uiputfile('*.txt','Exported Data File');
[pp fp ee] = fileparts([fopath foname]);
fofilename = [pp '\' fp '.txt'];
fout = fopen(fofilename, 'w');
if(fout == -1)
    fprintf('Unable to open output file: %s\n', fofilename);
    return;
end;
c = datac('control'); % get the data we need.
% find the selected data list
sel = getmainselection;
v = varargin{1};
fprintf(fout, 'Filename\tRecords');
for i = 2:nargin
    fprintf(fout,'\t%s.%s', varargin{1}, varargin{i});
end;
fprintf(fout, '\n');
liflag = 0;
z=[];
for ii = sel % run through all the data sets
    first = 1;

    for i = 2:nargin % selected variables
        if(check_field(c(ii), v) && ~isempty(eval(['c(ii).' v])))
            x = varargin{i};
            civ = eval(['c(ii).' v]);
            if(~isnan(eval(['civ.' x])))
                if(first)
                    fprintf(fout, '%s\t%d-%d', c(ii).filename, c(ii).recbeg, c(ii).recend);
                    first = 0;
                end
                if(length(eval(['civ.' x])) == 1 )
                    fprintf(fout, '\t%f', eval(['civ.' x]));
                else
                    z{i-1}=eval(['civ.' x]);
                    liflag = 1 ;
                end;
            end;
        end;
    end;
    if(liflag)
        fprintf(fout,'\n');
        u = size(z);

        v=size(z{1});
        for j = 1:v(1)
            for k = 1:u(2)
                fprintf(fout, '%f ',z{k}(j));
            end;
            fprintf(fout, '\n');
        end;
    end;



    if(first == 0)
        fprintf(fout, '\n');
    end;
end;
fclose(fout);
